package org.jcc.servlet.encoding.form.post;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/encoding/form/post/upload")
@MultipartConfig
public class Upload extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String english = request.getParameter("english");
		String chinese = request.getParameter("chinese");
		
		Part f1part = request.getPart("f1");
		String fileName = f1part.getSubmittedFileName();
		InputStream is = f1part.getInputStream();
		InputStreamReader reader = new InputStreamReader(is, "gbk");
		
		// output to client
		response.setContentType("text/html;charset=utf-8"); 
		PrintWriter writer = response.getWriter();
		writer.print(english + " " + chinese + "<br>");
		writer.print("文件名：" + fileName + "；文件内容：");
		int c;
		while((c = reader.read()) != -1) {
			writer.write((char) c);
		}
	}

	private static final long serialVersionUID = 1L;
}
